//
//  ICMCreateFileView.m
//  ICrEAM
//
//  Created by kuroro on 16/11/9.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ICMCreateFileView.h"
#import "ICMSaveReceptionVehicleOrderApi.h"
#import "PACACalendarView.h"
#import "NSDate+convenience.h"
#import "ProvinceCityRespose.h"

#define TitleTFViewHeight           ActualHeight(65)
#define xDistanceA                  ActualWidth(25)
#define xDistanceB                  ActualWidth(50)
#define yDistance                   (ScreenHeight-240-6*TitleTFViewHeight)/8

#define TitleTViewHeight            ActualHeight(90)

@implementation ICMCreateFileView

//获取品牌车系车型信息
- (void)getCarBrandData{
    self.carBrandDataArray = [ICrEAMTool queryBrandSeries];
    NSMutableArray *brandNameArray = [[NSMutableArray alloc] init];
    NSMutableArray *brandNoArray = [[NSMutableArray alloc] init];
    for(int i = 0;i<self.carBrandDataArray.count;i++)
    {
        BrandModel *brandData = self.carBrandDataArray[i];
        [brandNameArray addObject:brandData.BRAND_NAME];
        [brandNoArray addObject:brandData.BRAND_CODE];
    }
    self.carBrandView.clickDataArray = brandNameArray;
    self.carBrandView.clickDataNoArray = brandNoArray;
    
}
//获得省市县
- (void)getCityData{
    self.cityDataArray = [ICrEAMTool getProvinceCityArray];
    NSMutableArray *provinceNameArray = [[NSMutableArray alloc] init];
    NSMutableArray *provinceNoArray = [[NSMutableArray alloc] init];
    for(int i = 0;i<self.cityDataArray.count;i++)
    {
        NSArray *tempArray = self.cityDataArray[i];
        ProvinceModel *provinceData = tempArray[0];
        [provinceNameArray addObject:provinceData.provinceName];
        [provinceNoArray addObject:provinceData.provinceCode];
    }
    self.ownerShengView.clickDataArray = provinceNameArray;
    self.ownerShengView.clickDataNoArray = provinceNoArray;
}
- (instancetype)initICMCreateFileViewWithFrame:(CGRect)frame withType:(NSInteger)type{
    if (self = [super init]) {
        //        if ([ICrEAMTool isVW]) {
        //            self.brandStr=@"VW";
        //        }else{
        //            self.brandStr=@"SK";
        //        }
        self.frame = frame;
        
     
        self.bigScrollView = [[TPKeyboardAvoidingScrollView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        
        self.bigScrollView.backgroundColor = [UIColor clearColor];
        
        self.backgroundColor = [UIColor whiteColor];
        UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, ActualHeight(68))];
        [self.bigScrollView addSubview:headerView];
        
        UILabel *title = [[UILabel alloc]init];
        title.text = @"快速客户车辆信息登记";
        title.font = NaviFont;
        title.textColor = TitleColor;
        [headerView addSubview:title];
        [title mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(headerView.mas_centerX);
            make.centerY.equalTo(headerView.mas_centerY);
        }];
        
        UIView *lineView = [[UIView alloc]init];
        lineView.backgroundColor = TitleColor_Dark;
        [headerView addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(headerView.mas_left);
            make.right.equalTo(headerView.mas_right);
            make.bottom.equalTo(headerView.mas_bottom);
            make.height.equalTo(@(1));
        }];
        
        
        ICMTitleTextFeildView *vinNumberView = [ICMTitleTextFeildView createEditableICMTitleTextFeildViewWithFrame:CGRectMake(ActualWidth(25), ActualHeight(75), ActualWidth(185), TitleTFViewHeight) title:@"VIN"];
        vinNumberView.limitedNum = 17;
        vinNumberView.delegate=self;
        [self.bigScrollView addSubview:vinNumberView];
        self.vinNumberView = vinNumberView;
        self.vinNumberView.delegate = self;
        self.vinNumberView.isUppercase = YES;
        UIButton *synchronizeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [synchronizeButton setUpButtonWithNormolTitle:nil selectedTitle:nil normolTitleSize:0 selectedTitleSize:0 normolTitleColor:nil selectedTitleColor:nil normolBcakImage:[UIImage imageNamed:GetCar_Synchronize] selectedBackImage:nil actionSEL:@selector(clickSynchronizeButton) target:self];
        [self.bigScrollView addSubview:synchronizeButton];
        [synchronizeButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(vinNumberView.mas_right);
            make.top.equalTo(vinNumberView.mas_top);
            make.width.equalTo(@(ActualWidth(23)));
            make.height.equalTo(@(ActualWidth(23)));
        }];
        
        UIButton *qrcodeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [qrcodeBtn setUpButtonWithNormolTitle:nil selectedTitle:nil normolTitleSize:0 selectedTitleSize:0 normolTitleColor:nil selectedTitleColor:nil normolBcakImage:[UIImage imageNamed:GetCar_Scan] selectedBackImage:nil actionSEL:@selector(clickQRCodeBtn) target:self];
        [self.bigScrollView addSubview:qrcodeBtn];
        [qrcodeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(synchronizeButton.mas_left).offset(-ActualWidth(30));
            make.top.equalTo(vinNumberView.mas_top);
            make.width.equalTo(@(ActualWidth(23)));
            make.height.equalTo(@(ActualWidth(23)));
        }];
        
//        ICMTitleTextFeildView *selectAreaView = [ICMTitleTextFeildView createEditableICMTitleTextFeildViewWithFrame:CGRectMake(vinNumberView.rightEdge+ActualWidth(25), vinNumberView.y, ActualWidth(185), TitleTFViewHeight) title:@"车牌号"];
//        selectAreaView.contentTextFeild.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
//        
//        [self.bigScrollView addSubview:selectAreaView];
//        self.selectAreaView = selectAreaView;

        
        ICMTitleTextFeildView *selectAreaView = [ICMTitleTextFeildView createClickableICMTitleTextFeildViewWithFrame:CGRectMake(vinNumberView.rightEdge+xDistanceA, vinNumberView.y, ActualWidth(65), TitleTFViewHeight) title:@"车牌号" addTarget:self selector:@selector(clickSelectAreaView:)];
//        selectAreaView.contentTextFeild.placeholder = @"必填";
        [self.bigScrollView addSubview:selectAreaView];
        self.selectAreaView = selectAreaView;
        
        ICMTitleTextFeildView *plateNumberView = [ICMTitleTextFeildView createEditableICMTitleTextFeildViewWithFrame:CGRectMake(selectAreaView.rightEdge, selectAreaView.y, ActualWidth(125), TitleTFViewHeight) title:@" "];
        plateNumberView.contentTextFeild.keyboardType = UIKeyboardTypeDefault;
        plateNumberView.delegate = self;
        //        plateNumberView.limitedNum = 7;
        plateNumberView.contentTextFeild.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
        
        //        plateNumberView.contentTextFeild.placeholder = @"请选择";
        [self.bigScrollView addSubview:plateNumberView];
        self.plateNumberView = plateNumberView;
        
        
        
        
        
        ICMTitleTextFeildView *dateView = [ICMTitleTextFeildView createClickableICMTitleTextFeildViewWithFrame:CGRectMake(plateNumberView.rightEdge+ActualWidth(25), vinNumberView.y, ActualWidth(185), TitleTFViewHeight) title:@"购车发票日期" addTarget:self selector:@selector(clickCalendar)];
        [self.bigScrollView addSubview:dateView];
        self.dateView = dateView;
        ICMTitleTextFeildView *natureView = [ICMTitleTextFeildView createClickableICMTitleTextFeildViewWithFrame:CGRectMake(dateView.rightEdge+ActualWidth(25), vinNumberView.y, ActualWidth(185), TitleTFViewHeight) title:@"车辆性质" addTarget:self selector:@selector(clickNature)];
        
        
        [self.bigScrollView addSubview:natureView];
        self.natureView = natureView;
        [self.bigScrollView addSubview:self.carBrandView];
        [self.bigScrollView addSubview:self.carXiView];
        [self.bigScrollView addSubview:self.carTypeView];
        [self.bigScrollView addSubview:self.carlifeView];
        
        ICMTitleTextFeildView *nameView = [ICMTitleTextFeildView createEditableICMTitleTextFeildViewWithFrame:CGRectMake(ActualWidth(25), vinNumberView.bottomEdge+ActualHeight(110), vinNumberView.width, TitleTFViewHeight) title:@"车主姓名"];
        [self.bigScrollView addSubview:nameView];
        self.nameView = nameView;
        [self.bigScrollView addSubview:self.ownerPropertyView];
        
        
        ICMTitleTextFeildView *linkmanView = [ICMTitleTextFeildView createEditableICMTitleTextFeildViewWithFrame:CGRectMake(self.ownerPropertyView.rightEdge+ActualWidth(25), nameView.y, ActualWidth(185), TitleTFViewHeight) title:@"联系人姓名"];
        [self.bigScrollView addSubview:linkmanView];
        
        ICMTitleTextFeildView *linkmanMobelView = [ICMTitleTextFeildView createEditableICMTitleTextFeildViewWithFrame:CGRectMake(dateView.rightEdge+ActualWidth(25), nameView.y, ActualWidth(185), TitleTFViewHeight) title:@"联系人手机"];
        [self.bigScrollView addSubview:linkmanMobelView];
        linkmanMobelView.contentTextFeild.keyboardType = UIKeyboardTypePhonePad;
        self.linkmanView = linkmanView;
        self.linkmanMobelView = linkmanMobelView;
        
        [self.bigScrollView addSubview:self.ownerSexView];
        [self.bigScrollView addSubview:self.ownerShengView];
        [self.bigScrollView addSubview:self.ownerCityView];
        [self.bigScrollView addSubview:self.ownerTownView];
        
        
        
        ICMBottomBarView *bottomBarView = [ICMBottomBarView createBottomBarViewWithTitles:@[@"保 存"] normalImages:@[[UIImage imageNamed:@"btn_save"]] seletedImages:nil andFrame:CGRectMake(0, ActualHeight(490)-70, ActualWidth(940), 70)];
        bottomBarView.delegate = self;
        [self.bigScrollView addSubview:bottomBarView];
        
        
        self.bigScrollView.contentSize = CGSizeMake(ScreenWidth, bottomBarView.bottomEdge);
        [self addSubview:self.bigScrollView];
        [self getCarBrandData];
        [self getCityData];
        if (!self.customModel) {
            self.customModel=[[CustomRecordModel alloc]init];
            
        }
        //[ICrEAMTool showPopoverSelectionViewWithSourceView:self.repairTypeLabel displayWidth:self.repairTypeLabel.width displayHeight:self.height dataArray:self.repairTypes delegate:self showType:PopoverSelectionViewShowTypeTable];
        
    }
    return self;
}
-(void)setCustomModel:(CustomRecordModel *)customModel{
    _customModel = customModel;
    if (customModel) {
        self.vinNumberView.contentText=customModel.VIN;
        if (_customModel.LICENSE.length>2){
        NSString *area = [_customModel.LICENSE substringWithRange:NSMakeRange(0, 1)];
        NSString *plant = [_customModel.LICENSE substringWithRange:NSMakeRange(1, _customModel.LICENSE.length-1)];
        unsigned char mCode;
        sscanf([area UTF8String], "%c", &mCode);
        
        if (mCode <= 'Z' && mCode >= 'A') {
            area = [_customModel.LICENSE substringWithRange:NSMakeRange(0, 2)];
            plant = [_customModel.LICENSE substringWithRange:NSMakeRange(2, customModel.LICENSE.length-2)];
        }
            self.selectAreaView.contentText = area;
            self.plateNumberView.contentText = plant;
        }
        
//        self.selectAreaView.contentText=customModel.LICENSE;
        self.nameView.contentText=customModel.OWNER_NAME;
        self.linkmanView.contentText=customModel.CONTACTOR_NAME;
        self.linkmanMobelView.contentText=customModel.CONTACTOR_MOBILE;
        self.dateView.contentText=customModel.SALES_DATE;
        
        ParameterModel *natureModel=[ICrEAMTool getCodeModelForCode:customModel.BUSINESS_KIND];
        self.natureView.contentText=natureModel.desc;
        self.natureView.noStr =customModel.carNature;
        
        self.carBrandView.contentText=customModel.BRAND_NAME;
        self.carBrandView.noStr = customModel.BRAND_CODE;
        self.carXiView.contentText=customModel.SERIES_NAME;
        self.carXiView.noStr = customModel.SERIES_CODE;
        self.carTypeView.contentText=customModel.MODEL_NAME;
        self.carTypeView.noStr = customModel.MODEL_CODE;
        
        ParameterModel *lifestaModel=[ICrEAMTool getCodeModelForCode:customModel.VEHICLE_LIFE_STATUS];
        self.carlifeView.contentText=lifestaModel.desc;
        
        ParameterModel *genderModel=[ICrEAMTool getCodeModelForCode:customModel.GENDER];
        self.ownerSexView.contentText=genderModel.desc;
        self.ownerSexView.noStr=genderModel.code;
        
        self.ownerShengView.contentText=[self getCityDataWithCode:customModel.PROVINCE Type:0];
        self.ownerShengView.noStr = customModel.PROVINCE;
        
        self.ownerCityView.contentText=[self getCityDataWithCode:customModel.CITY Type:1];
        self.ownerCityView.noStr = customModel.CITY;
        self.ownerTownView.contentText=[self getCityDataWithCode:customModel.DISTRICT Type:2];
        self.ownerTownView.noStr = customModel.DISTRICT;
         ParameterModel *onwerNatureModel=[ICrEAMTool getCodeModelForCode:customModel.OWNER_PROPERTY];
        self.ownerPropertyView.contentText=onwerNatureModel.desc;
        self.ownerPropertyView.noStr =customModel.OWNER_PROPERTY;
        
        
        
        
    }else{
    
        _customModel=[[CustomRecordModel alloc]init];
    }
    
    
}

//品牌
- (ICMTitleTextFeildView *)carBrandView
{
    if(!_carBrandView)
    {
        _carBrandView = [ICMTitleTextFeildView createClickableICMTitleTextFeildViewWithFrame:CGRectMake(self.vinNumberView.leftEdge, self.vinNumberView.bottomEdge + ActualHeight(20), self.vinNumberView.width, ActualHeight(63)) title:@"品牌" addTarget:nil selector:nil];
        _carBrandView.delegate = self;
        
    }
    return _carBrandView;
}

//车型
- (ICMTitleTextFeildView *)carTypeView
{
    if(!_carTypeView)
    {
        _carTypeView = [ICMTitleTextFeildView createClickableICMTitleTextFeildViewWithFrame:CGRectMake(self.dateView.leftEdge, self.vinNumberView.bottomEdge + ActualHeight(20),self.vinNumberView.width, ActualHeight(63)) title:@"车型" addTarget:nil selector:nil];
        _carTypeView.delegate = self;
        
    }
    return _carTypeView;
}

//车系
- (ICMTitleTextFeildView *)carXiView
{
    if(!_carXiView)
    {
        _carXiView = [ICMTitleTextFeildView createClickableICMTitleTextFeildViewWithFrame:CGRectMake(self.selectAreaView.leftEdge, self.vinNumberView.bottomEdge + ActualHeight(20),self.vinNumberView.width, ActualHeight(63)) title:@"车系" addTarget:nil selector:nil];
        _carXiView.delegate = self;
        
    }
    return _carXiView;
}
//车主性别
- (ICMTitleTextFeildView *)ownerSexView
{
    if(!_ownerSexView)
    {
        _ownerSexView = [ICMTitleTextFeildView createClickableICMTitleTextFeildViewWithFrame:CGRectMake(self.vinNumberView.leftEdge,self.ownerPropertyView.bottomEdge+ActualHeight(25), self.vinNumberView.width, ActualHeight(63)) title:@"车主性别" addTarget:nil selector:nil];
        NSArray *cleanCarDataArray = [ICrEAMTool getParameterArrayWithCode:ICMEnumIntToString(ICMEnumGenderPre)];
        _ownerSexView.clickDataArray = [[ICrEAMTool getParameterTitleArrayWithArray:cleanCarDataArray] mutableCopy];
        
    }
    return _ownerSexView;
}

//车辆生命状态
- (ICMTitleTextFeildView *)carlifeView
{
    if(!_carlifeView)
    {
        _carlifeView = [ICMTitleTextFeildView createClickableICMTitleTextFeildViewWithFrame:CGRectMake(self.natureView.leftEdge,self.carTypeView.topEdge, self.vinNumberView.width, ActualHeight(63)) title:@"车辆生命状态" addTarget:nil selector:nil];
        NSArray *cleanCarDataArray = [ICrEAMTool getParameterArrayWithCode:ICMEnumIntToString(ICMEnumCarLifePre)];
        _carlifeView.clickDataArray = [[ICrEAMTool getParameterTitleArrayWithArray:cleanCarDataArray] mutableCopy];
        
    }
    return _carlifeView;
}
//车主性质
- (ICMTitleTextFeildView *)ownerPropertyView
{
    if(!_ownerPropertyView)
    {
        _ownerPropertyView = [ICMTitleTextFeildView createClickableICMTitleTextFeildViewWithFrame:CGRectMake(self.vinNumberView.rightEdge+ActualWidth(25), self.nameView.y,  self.vinNumberView.width, ActualHeight(63)) title:@"车主性质" addTarget:nil selector:nil];
        NSArray *cleanCarDataArray = [ICrEAMTool getParameterArrayWithCode:ICMEnumIntToString(ICMEnumOwnerPropertyPre)];
        _ownerPropertyView.clickDataArray = [[ICrEAMTool getParameterTitleArrayWithArray:cleanCarDataArray] mutableCopy];
        
    }
    return _ownerPropertyView;
}
//所在省
- (ICMTitleTextFeildView *)ownerShengView
{
    if(!_ownerShengView)
    {
        _ownerShengView = [ICMTitleTextFeildView createClickableICMTitleTextFeildViewWithFrame:CGRectMake(self.ownerPropertyView.leftEdge,self.ownerSexView.topEdge, self.vinNumberView.width, ActualHeight(63)) title:@"省份" addTarget:nil selector:nil];
        _ownerShengView.delegate = self;
        
    }
    return _ownerShengView;
}
- (NSArray *)cityDataArray
{
    if(!_cityDataArray)
    {
        _cityDataArray = [[NSArray alloc] init];
    }
    return _cityDataArray;
}
- (NSArray *)carBrandDataArray
{
    if(!_carBrandDataArray)
    {
        _carBrandDataArray = [[NSArray alloc] init];
    }
    return _carBrandDataArray;
}
#pragma mark 根据车系code查找车系list
- (void)setupModelListWithBrandCode:(NSString *)bcode WithSeriesCode:(NSString *)scode
{
    NSMutableArray *modelNameArray = [[NSMutableArray alloc] init];
    NSMutableArray *modelNoArray = [[NSMutableArray alloc] init];
    for(int i = 0;i<self.carBrandDataArray.count;i++)
    {
        BrandModel *brandData = self.carBrandDataArray[i];
        if([brandData.BRAND_CODE isEqualToString:bcode])
        {
            NSArray *tempSeriesArray = brandData.TM_SERIES;
            for(int j = 0;j<tempSeriesArray.count;j++)
            {
                SeriesModel *serieddata = tempSeriesArray[j];
                if([serieddata.SERIES_CODE isEqualToString:scode])
                {
                    NSArray *tempModelArray = serieddata.TM_MODEL;
                    for(int z = 0;z<tempModelArray.count;z++)
                    {
                        TMModel *modelData = tempModelArray[z];
                        [modelNameArray addObject:modelData.MODEL_NAME];
                        [modelNoArray addObject:modelData.MODEL_CODE];
                    }
                }
                
                
            }
        }
        
    }
    self.carTypeView.clickDataArray = modelNameArray;
    self.carTypeView.clickDataNoArray = modelNoArray;
    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"changeCarXi" object:nil userInfo:nil];
    
    
    
}

#pragma mark 根据品牌code查找品牌list
- (void)setupSeriesListWithBrandCode:(NSString *)code
{
    NSMutableArray *seriesNameArray = [[NSMutableArray alloc] init];
    NSMutableArray *seriesNoArray = [[NSMutableArray alloc] init];
    for(int i = 0;i<self.carBrandDataArray.count;i++)
    {
        BrandModel *brandData = self.carBrandDataArray[i];
        if([brandData.BRAND_CODE isEqualToString:code])
        {
            NSArray *tempSeriesArray = brandData.TM_SERIES;
            for(int j = 0;j<tempSeriesArray.count;j++)
            {
                SeriesModel *serieddata = tempSeriesArray[j];
                [seriesNameArray addObject:serieddata.SERIES_NAME];
                [seriesNoArray addObject:serieddata.SERIES_CODE];
            }
        }
        
    }
    self.carXiView.clickDataArray = seriesNameArray;
    self.carXiView.clickDataNoArray = seriesNoArray;
}

//所在市
- (ICMTitleTextFeildView *)ownerCityView
{
    if(!_ownerCityView)
    {
        _ownerCityView = [ICMTitleTextFeildView createClickableICMTitleTextFeildViewWithFrame:CGRectMake(self.linkmanView.x,self.ownerSexView.topEdge, self.vinNumberView.width, ActualHeight(63)) title:@"城市" addTarget:nil selector:nil];
        _ownerCityView.delegate = self;
        
    }
    return _ownerCityView;
}
//ICMTitleTextFeildViewDelegate
- (void)clickedData:(ICMTitleTextFeildView *)titleTextFeildView
{
    if(titleTextFeildView == self.ownerShengView)
    {
        self.ownerCityView.contentText = @"";
        self.ownerCityView.noStr = @"";
        
        self.ownerTownView.contentText = @"";
        self.ownerTownView.noStr = @"";
        
        [self setupCityListWithProvinceCode:self.ownerShengView.noStr];
    }
    if(titleTextFeildView == self.ownerCityView)
    {
        self.ownerTownView.contentText = @"";
        self.ownerTownView.noStr = @"";
        [self setupDistrictyListWithProvinceCode:self.ownerShengView.noStr WithCityCode:self.ownerCityView.noStr];
    }
    if(titleTextFeildView == self.carBrandView)
    {
        self.carXiView.contentText = @"";
        self.carXiView.noStr = @"";
        self.carTypeView.contentText = @"";
        self.carTypeView.noStr = @"";
        [self setupSeriesListWithBrandCode:self.carBrandView.noStr];
    }
    if(titleTextFeildView == self.carXiView)
    {
        self.carTypeView.contentText = @"";
        self.carTypeView.noStr = @"";
        [self setupModelListWithBrandCode:self.carBrandView.noStr WithSeriesCode:self.carXiView.noStr];
        
    }
}
//所在区县
- (ICMTitleTextFeildView *)ownerTownView
{
    if(!_ownerTownView)
    {
        _ownerTownView = [ICMTitleTextFeildView createClickableICMTitleTextFeildViewWithFrame:CGRectMake(self.linkmanMobelView.x, self.ownerSexView.topEdge, self.vinNumberView.width, ActualHeight(63)) title:@"区县" addTarget:nil selector:nil];
        
    }
    return _ownerTownView;
}

+ (instancetype)creatICMCreateFileViewWithFrame:(CGRect)frame withType:(NSInteger)type{
    return [[ICMCreateFileView alloc]initICMCreateFileViewWithFrame:frame withType:type];
}
-(void)textFiledShouldReturnClickWithView:(ICMTitleTextFeildView *)textView{
    BOOL isVin=YES;
    if (textView==self.selectAreaView) {
        isVin=NO;
    }
    if ([self.delegate respondsToSelector:@selector(textFiledShouldReturnWithText:isVin:)]) {
        [self.delegate textFiledShouldReturnWithText:textView.contentText isVin:isVin];
    }
}
- (void)clickNature{
    NSArray *cleanCarDataArray = [ICrEAMTool getParameterArrayWithCode:@"2201"];
    ICMSelecteItemDataView *dataView = [ICMSelecteItemDataView createSelecteItemDataViewWithSourceView:self.natureView andItemDatas:[[ICrEAMTool getParameterTitleArrayWithArray:cleanCarDataArray] mutableCopy] frame:self.frame];
    dataView.delegate = self;
    [self addSubview:dataView];
}

-(void)clickCalendar{
    PACACalendarView *calendarView = [PACACalendarView createCalendarViewWithDidSelectedDate:self.selectedDate minDate:nil maxDate:nil andFrame:CGRectMake(ScreenWidth/2-150, ScreenHeight/2-150, ActualWidth(300), ActualWidth(380))];
    calendarView.delegate = self;
    [Window addSubview:calendarView];
    
}

- (void)clickQRCodeBtn{
    if ([self.delegate respondsToSelector:@selector(clickQRCodeBtnWithType:)]) {
        [self.delegate clickQRCodeBtnWithType:1];
    }
    
}

- (void)didSelectedItemTitle:(NSString *)itemTitle{
    self.natureView.contentText = itemTitle;
}

- (void)bottomBarViewDidSelectIndex:(NSInteger)index{
    if ([self checkFormData]) {
        if ([self.delegate respondsToSelector:@selector(clickSave:)]) {
            [self save];
        }
    }
}

//表单字段验证
- (BOOL)checkFormData
{
    BOOL success = YES;
    NSString *errMsg = @"";
    if(_type == 1){
        
        //车牌号
        if(StrIsZero_gdm(self.selectAreaView.contentText))
        {
            success = NO;
            errMsg = @"车牌号不能为空!";
        }
        //联系人姓名
        if(StrIsZero_gdm(self.linkmanView.contentText))
        {
            success = NO;
            errMsg = @"联系人姓名不能为空!";
        }
        //联系人手机
        if(StrIsZero_gdm(self.linkmanMobelView.contentText))
        {
            success = NO;
            errMsg = @"联系人手机不能为空!";
        }

        
    }else{
    
        //车主姓名
        if(StrIsZero_gdm(self.nameView.contentText))
        {
            success = NO;
            errMsg = @"车主姓名不能为空!";
        }
        //联系人姓名
        if(StrIsZero_gdm(self.linkmanView.contentText))
        {
            success = NO;
            errMsg = @"联系人姓名不能为空!";
        }
        //联系人手机
        if(StrIsZero_gdm(self.linkmanMobelView.contentText))
        {
            success = NO;
            errMsg = @"联系人手机不能为空!";
        }
        //车辆性质
        if(StrIsZero_gdm(self.natureView.contentText))
        {
            success = NO;
            errMsg = @"车辆性质不能为空!";
        }
        //车牌号
        if(StrIsZero_gdm(self.selectAreaView.contentText))
        {
            success = NO;
            errMsg = @"车牌号不能为空!";
        }
        //车辆生命周期
        if(StrIsZero_gdm(self.carlifeView.contentText))
        {
            success = NO;
            errMsg = @"车辆生命周期不能为空!";
        }

        //购车日期
        if(StrIsZero_gdm(self.dateView.contentText))
        {
            success = NO;
            errMsg = @"购车日期不能为空!";
        }

    
    }

    
    
    
        //发送通知显示msg
    [[NSNotificationCenter defaultCenter] postNotificationName:@"CreateFileMsgNotifacation" object:self userInfo:@{@"msg":errMsg}];
    return success;
}

- (void)editableViewDidEndEdit:(ICMTitleTextFeildView *)sender{
    BOOL isVin=YES;
    if (sender ==self.selectAreaView) {
        isVin=NO;
    }
    if(_vinNumberView&&self.selectAreaView){
        
        if ([self.delegate respondsToSelector:@selector(textFiledShouldReturnWithText:isVin:)]) {
            [self.delegate textFiledShouldReturnWithText:sender.contentText isVin:isVin];
        }
        
        
        
    }
    
}
//同步XXX系统
- (void)clickSynchronizeButton{
    if(!StrIsZero_gdm(self.vinNumberView.contentText)){
        ICMRequestManager *manager = [[ICMRequestManager alloc]init];
        NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
        [parameters setObject:self.vinNumberView.contentText forKey:@"VIN"];
        [manager startGetRequestWithURL:SyncVehicleInfo parameters:parameters inHudView:self andResultBlock:^(id data, NSString *failText) {
            if ([data[@"CODE"] integerValue] == 200) {
                if (IsArr(data[@"TM_VEHICLE"])) {
                    self.customModel = [CustomRecordModel yy_modelWithDictionary:data[@"TM_VEHICLE"][0]];
                    NSString *subAreaStr = [self.customModel.LICENSE substringToIndex:1];
                    NSString *subPlateStr = [self.customModel.LICENSE substringFromIndex:1];
                    self.selectAreaView.contentText = subAreaStr;
                    self.plateNumberView.contentText = subPlateStr;
                    self.dateView.contentText = [ICrEAMTool getDateWithTimeInterval:self.customModel.SALES_DATE];
                    self.nameView.contentText = self.customModel.OWNER_NAME;
                    self.linkmanView.contentText = self.customModel.CONTACTOR_NAME;
                    self.linkmanMobelView.contentText = self.customModel.CONTACTOR_MOBILE;
                }
            }
        }];
    }
}
-(NSString*)getTextFieldAreaAndPlate{
    
    //获取车牌号输入框的数据
    NSString *areaAndPlant = [NSString stringWithFormat:@"%@%@",self.selectAreaView.contentText?:@"",self.plateNumberView.contentText?:@""];
    
    return areaAndPlant;
    
}


- (void)save{
    
    
 
    
    self.customModel.OWNER_NAME = self.nameView.contentText;
    self.customModel.CONTACTOR_NAME = self.linkmanView.contentText;
    self.customModel.CONTACTOR_MOBILE = self.linkmanMobelView.contentText;
    self.customModel.VIN = self.vinNumberView.contentText;
    self.customModel.LICENSE = [self getTextFieldAreaAndPlate];
    self.customModel.SALES_DATE = self.dateView.contentText;
    self.customModel.BUSINESS_KIND = self.natureView.contentText;
    
    NSString *owner_sex = [ICrEAMTool getDeatilCodeWithParameterDesc:self.ownerSexView.contentText withCode:ICMEnumIntToString(ICMEnumGenderPre)];
    self.customModel.GENDER=owner_sex?owner_sex:@"0";
    
    NSString *ownerType = [ICrEAMTool getDeatilCodeWithParameterDesc:self.ownerPropertyView.contentText withCode:ICMEnumIntToString(ICMEnumOwnerPropertyPre)];
    self.customModel.OWNER_PROPERTY = ownerType?ownerType:@"0";
    self.customModel.PROVINCE = self.ownerShengView.noStr;
    self.customModel.CITY = self.ownerCityView.noStr;
    self.customModel.DISTRICT = self.ownerTownView.noStr;
    
    NSString *carLifeType = [ICrEAMTool getDeatilCodeWithParameterDesc:self.carlifeView.contentText withCode:ICMEnumIntToString(ICMEnumCarLifePre)];
    self.customModel.VEHICLE_LIFE_STATUS=carLifeType?carLifeType:@"0";
    //品牌
    self.customModel.BRAND_CODE = self.carBrandView.noStr;
    self.customModel.BRAND_NAME=self.carBrandView.contentText;
    self.customModel.SERIES_CODE = self.carXiView.noStr;
    self.customModel.SERIES_NAME=self.carXiView.contentText;
    //车型
    self.customModel.MODEL_CODE = self.carTypeView.noStr;
    self.customModel.MODEL_NAME = self.carTypeView.contentText;
    
    if ([self.ownerPropertyView.contentText isEqualToString:@"公司"]) {
        self.customModel.OWNER_PROPERTY = @"11901001";
        
    }else if ([self.ownerPropertyView.contentText isEqualToString:@"个人"]){
        self.customModel.OWNER_PROPERTY = @"11901002";
        
    }else if ([self.ownerPropertyView.contentText isEqualToString:@"政府"]){
        self.customModel.OWNER_PROPERTY = @"11901004";
        
    }else{
        self.customModel.OWNER_PROPERTY = @"11901003";
    }
    
    
    //车辆性质
    NSString *carNature = [ICrEAMTool getDeatilCodeWithParameterDesc:self.natureView.contentText withCode:@"2201"];
    self.customModel.BUSINESS_KIND = carNature?carNature:@"0";
    
    
    
    /*
    NSString *url = [NSString stringWithFormat:@"/INFO_ICREAM_WEB/App/maintainOwnerAndVehicleInfo"];
    [[[ICMRequestManager alloc]init]startPostRequestWithURL:url parameters:[self setupSaveAppointmentData] inHudView:self andResultBlock:^(id data, NSString *failText) {
        if ([data[@"CODE"] integerValue]== 200) {
            [self.delegate clickSave:self.customModel];
            //发送通知显示msg
            [[NSNotificationCenter defaultCenter] postNotificationName:@"CreateFileMsgNotifacation" object:self userInfo:@{@"msg":@"保存成功!"}];
        }
        else
        {
            //发送通知显示msg
            [[NSNotificationCenter defaultCenter] postNotificationName:@"CreateFileMsgNotifacation" object:self userInfo:@{@"msg":@"客户档案已存在!"}];
        }
    }];
     */
    
    if([ownerType isEqualToString:@"11901002"]){
        if([ICrEAMTool isVaildRealName:self.nameView.contentText] == NO ){
            
            [self showString:@"车主姓名有误，请修改后保存！" forSeconds:1.5];
            return ;
        }
    }
    if([ownerType isEqualToString:@"11901001"]){
        if([ICrEAMTool isVaildCompanyName:self.nameView.contentText] == NO ){
            
            [self showString:@"公司名称有误，请修改后保存！" forSeconds:1.5];
            return ;
        }
    }
    //    if([ICrEAMTool validateMobile:self.proxyBasicInfoView.ownerPhoneNumberView.contentText] == NO){
    //
    //        [self.view showString:@"车主手机号有误，请修改后保存！" forSeconds:1.5];
    //        return NO;
    //    }
    if([ICrEAMTool isVaildRealName:self.linkmanView.contentText] == NO ){
        
        [self showString:@"联系人姓名有误，请修改后保存！" forSeconds:1.5];
        return ;
    }
    if([ICrEAMTool validateMobile:self.linkmanMobelView.contentText] == NO){
        
        [self showString:@"联系人手机号有误，请修改后保存！" forSeconds:1.5];
        return ;
    }
    
    [self.delegate clickSave:self.customModel];
}

- (NSMutableDictionary *)setupSaveAppointmentData{
    
    
    NSMutableDictionary *dataDic = [NSMutableDictionary dictionary];
    NSDictionary *basicInfoModelDic = [self.customModel yy_modelToJSONObject];
    [dataDic setObject:@[basicInfoModelDic] forKey:@"TM_CUSTOMER_RECORD"];
    
    return dataDic;
}


/*ICMDatePickerViewDelegate*/
- (void)calendarView:(PACACalendarView *)calendarView didSelectedDate:(NSDate *)selectedDate{
    self.selectedDate = selectedDate;
    NSString *dateString = [selectedDate separatorStyleDateTimeString];
    self.dateView.contentTextFeild.text = dateString;
    //获得可预约时间列表
}

//清空时间
- (void)delTime:(PACACalendarView *)calendarView{
    self.selectedDate = nil;
    self.dateView.contentTextFeild.text = @"";
}
//根据省code查找citylist
- (void)setupCityListWithProvinceCode:(NSString *)code
{
    NSMutableArray *cityNameArray = [[NSMutableArray alloc] init];
    NSMutableArray *cityNoArray = [[NSMutableArray alloc] init];
    for(int i = 0;i<self.cityDataArray.count;i++)
    {
        NSArray *tempProvinceArray = self.cityDataArray[i];
        ProvinceModel *provinceData = tempProvinceArray[0];
        if([provinceData.provinceCode isEqualToString:code])
        {
            NSArray *tempCityArray = provinceData.cityList;
            for(int j = 0;j<tempCityArray.count;j++)
            {
                CityModel *citydata = tempCityArray[j];
                [cityNameArray addObject:citydata.cityName];
                [cityNoArray addObject:citydata.cityCode];
            }
        }
        
    }
    self.ownerCityView.clickDataArray = cityNameArray;
    self.ownerCityView.clickDataNoArray = cityNoArray;
}

//根据city code查找districty list
- (void)setupDistrictyListWithProvinceCode:(NSString *)pcode WithCityCode:(NSString *)ccode
{
    NSMutableArray *disNameArray = [[NSMutableArray alloc] init];
    NSMutableArray *disNoArray = [[NSMutableArray alloc] init];
    for(int i = 0;i<self.cityDataArray.count;i++)
    {
        NSArray *tempProvinceArray = self.cityDataArray[i];
        ProvinceModel *provinceData = tempProvinceArray[0];
        if([provinceData.provinceCode isEqualToString:pcode])
        {
            NSArray *tempCityArray = provinceData.cityList;
            for(int j = 0;j<tempCityArray.count;j++)
            {
                CityModel *citydata = tempCityArray[j];
                if([citydata.cityCode isEqualToString:ccode])
                {
                    NSArray *tempDisArray = citydata.districtyList;
                    for(int z = 0;z<tempDisArray.count;z++)
                    {
                        DistrictModel *disData = tempDisArray[z];
                        [disNameArray addObject:disData.distName];
                        [disNoArray addObject:disData.distCode];
                    }
                }
            }
        }
        
    }
    self.ownerTownView.clickDataArray = disNameArray;
    self.ownerTownView.clickDataNoArray = disNoArray;
}

//根据code来查找省市县区
- (NSString *)getCityDataWithCode:(NSString *)code Type:(int)type
{
    self.cityDataArray = [ICrEAMTool getProvinceCityArray];
    if(type == 0)
    {
        //查询省
        for(int i = 0;i<self.cityDataArray.count;i++)
        {
            NSArray *tempProvinceArray = self.cityDataArray[i];
            ProvinceModel *provinceData = tempProvinceArray[0];
            if([provinceData.provinceCode isEqualToString:code])
            {
                return provinceData.provinceName;
            }
            
        }
    }
    if(type == 1)
    {
        //查询市
        for(int i = 0;i<self.cityDataArray.count;i++)
        {
            NSArray *tempProvinceArray = self.cityDataArray[i];
            ProvinceModel *provinceData = tempProvinceArray[0];
            NSArray *tempCityArray = provinceData.cityList;
            for(int j = 0;j<tempCityArray.count;j++)
            {
                CityModel *citydata = tempCityArray[j];
                if([citydata.cityCode isEqualToString:code])
                {
                    return citydata.cityName;
                }
            }
            
        }
    }
    if(type == 2)
    {
        //查询县区
        for(int i = 0;i<self.cityDataArray.count;i++)
        {
            NSArray *tempProvinceArray = self.cityDataArray[i];
            ProvinceModel *provinceData = tempProvinceArray[0];
            NSArray *tempCityArray = provinceData.cityList;
            for(int j = 0;j<tempCityArray.count;j++)
            {
                CityModel *citydata = tempCityArray[j];
                NSArray *tempDisArray = citydata.districtyList;
                for(int z = 0;z<tempDisArray.count;z++)
                {
                    DistrictModel *disData = tempDisArray[z];
                    if([disData.distCode isEqualToString:code])
                    {
                        return disData.distName;
                    }
                }
            }
            
        }
    }
    return @"";
}

#pragma mark    点击车牌地
- (void)clickSelectAreaView:(ICMTitleTextFeildView *)view {
    [self addSubview:self.selectAreaPrefixView];
}
#pragma mark selectAreaPrefixView
- (SelectAreaPrefixView *)selectAreaPrefixView {
    if (!_selectAreaPrefixView) {
        _selectAreaPrefixView = [[SelectAreaPrefixView alloc] initWithFrame:CGRectMake(ActualWidth(207), self.selectAreaView.bottomEdge, ActualWidth(630), ActualHeight(185))];
        _selectAreaPrefixView.delegate = self;
    }
    return _selectAreaPrefixView;
}
#pragma mark    选取车牌地
- (void)didSelectAreaPrefix:(NSString *)prefix{
    if ([prefix isEqualToString:@"其他"]) {
        [Window addSubview:self.inputAreaView];
        [self.inputAreaView.inputAreaTextView.contentTextFeild becomeFirstResponder];
        return;
    }
    //    BOOL isChange = ![self.selectAreaView.contentText isEqualToString:prefix];
    self.selectAreaView.contentText = prefix;
    [self.plateNumberView.contentTextFeild becomeFirstResponder];
    //    if (isChange&&self.plateNumberView.contentText.length>5&&[self.delegate respondsToSelector:@selector(licenseDidEndEdit:type:)]) {
    //        [self.delegate licenseDidEndEdit:self.plateNumberView type:@"1"];
    //    }
}
- (UIView *)inputAreaView{
    if (!_inputAreaView) {
        _inputAreaView = [ICMInputAreaView createInputAreaView];
        _inputAreaView.delegate = self;
    }
    return _inputAreaView;
}

#pragma mark    其他车牌地输入结束
- (void)inputAreaViewWillDisappearWithText:(NSString *)text{
    [self didSelectAreaPrefix:text];
}

@end
